package edu.lsnu.equipment_management_system.controller;

import edu.lsnu.equipment_management_system.exception.*;
import edu.lsnu.equipment_management_system.pojo.domain.JsonData;
import edu.lsnu.equipment_management_system.pojo.po.entity.UserTablePo;
import edu.lsnu.equipment_management_system.pojo.vo.in.*;
import edu.lsnu.equipment_management_system.service.TransactionalCollectionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

/**
 * @author mzy
 * @Description 所有提交数据接收数据的controller，非get方式请求了！
 * @date 2018/11/26
 */
@RestController
public class FormController {
    // @Autowired
    // TableOperationService tableOperationService;
    @Autowired
    TransactionalCollectionService transactionalCollectionService;
    /**
     * @Description 管理员增加用户
     * @author mzy
     * @time 2018-11-26
     * @see
     * @return   java.lang.Object
     * @params  []
     */
    @PostMapping("/api/admin/addUser-post")
    public Object adminAddUser(UserTablePo userTablePo) {
        boolean res = false;
        try {
            res = transactionalCollectionService.adminAddUser(userTablePo);
        } catch (NoExistException e) {
            return JsonData.buildError(e.getMessage());
        } catch (UserNoException e) {
            return JsonData.buildError(e.getMessage());
        } catch (NotNumericException e) {
            return JsonData.buildError(e.getMessage());
        }

        if (res) {
            return JsonData.buildSuccess(0, "ok", "操作成功");
        } else {
            return JsonData.buildError("操作失败");
        }
    }

    /**
     * @Description 用户提交报损申请
     * @author mzy
     * @time 2018-11-26
     * @see
     * @return   java.lang.Object
     * @params  []
     */
    @PostMapping("/api/user/damage-post")
    public Object userEquipmentDamage(EquipmentDamageFromPage equipmentDamageFromPage,
                                      HttpSession session) {
        boolean res = false;
        try {
            res = transactionalCollectionService.userEquipmentDamage(equipmentDamageFromPage, session);
        } catch (NoNotExistException e) {
            return JsonData.buildError("操作失败，报损设备编号不存在！");
        } catch (NoPowerException e) {
            return JsonData.buildError(e.getMessage());
        }

        if (res) {
            return JsonData.buildSuccess(0, "ok", "操作成功");
        } else {
            return JsonData.buildError("操作失败");
        }
    }

    /**
     * @Description 用户购买设备，并提交使用
     * @author mzy
     * @time 2018-11-26
     * @see
     * @return   java.lang.Object
     * @params  []
     */
    @PostMapping("/api/user/purchase-post")
    public Object userEquipmentPurchase(EquipmentPurchaseFromPage equipmentPurchaseFromPage,
                                        HttpSession session) {
        boolean res = false;
        try {
            res = transactionalCollectionService.userEquipmentPurchase(equipmentPurchaseFromPage,
                    session);
        } catch (NoExistException e) {
            return JsonData.buildError(e.getMessage());
        } catch (EquipmentNoSizeNotEqualNumberException e) {
            return JsonData.buildError(e.getMessage());
        } catch (InputEquipmentNoRepeatException e) {
            return JsonData.buildError(e.getMessage());
        } catch (NotNumericException e) {
            return JsonData.buildError(e.getMessage());
        } catch (EquipmentNoEexception e) {
            return JsonData.buildError(e.getMessage());
        } catch (InsertIntoEquipmentUseRecordTableException e) {
            return JsonData.buildError(e.getMessage());
        }


        if (res) {
            return JsonData.buildSuccess(0, "ok", "操作成功");
        } else {
            return JsonData.buildError("操作失败");
        }
    }

    /**
     * @Description 用户调入设备，并提交使用
     * @author mzy
     * @time 2018-11-26
     * @see
     * @return   java.lang.Object
     * @params  []
     */
    @PostMapping("/api/user/transferIn-post")
    public Object userEquipmentTransferIn(EquipmentTransferInFromPage equipmentTransferInFromPage,
                                          HttpSession session) {
        boolean res = false;
        try {
            res = transactionalCollectionService.userEquipmentTransferIn(equipmentTransferInFromPage, session);
        } catch (NoExistException e) {
            return JsonData.buildError(e.getMessage());
        } catch (EquipmentNoSizeNotEqualNumberException e) {
            return JsonData.buildError(e.getMessage());
        } catch (InputEquipmentNoRepeatException e) {
            return JsonData.buildError(e.getMessage());
        } catch (NotNumericException e) {
            return JsonData.buildError(e.getMessage());
        } catch (EquipmentNoEexception e) {
            return JsonData.buildError(e.getMessage());
        } catch (InsertIntoEquipmentUseRecordTableException e) {
            return JsonData.buildError(e.getMessage());
        }

        if (res) {
            return JsonData.buildSuccess(0, "ok", "操作成功");
        } else {
            return JsonData.buildError("操作失败");
        }
    }

    /**
     * @Description 用户维修设备申请
     * @author mzy
     * @time 2018-11-26
     * @see 
     * @return   java.lang.Object
     * @params  []
     */
    @PostMapping("/api/user/repair-post")
    public Object userEquipmentRepair(EquipmentRepairFromPage equipmentRepairFromPage,
                                      HttpSession session) {
        boolean res = false;
        try {
            res = transactionalCollectionService.userEquipmentRepair(equipmentRepairFromPage,
                    session);
        } catch (NoNotExistException e) {
            return JsonData.buildError("设备编号不存在");
        } catch (NoPowerException e) {
            return JsonData.buildError(e.getMessage());
        } catch (InsertIntoEquipmentRepairTableException e) {
            return JsonData.buildError(e.getMessage());
        }

        if (res) {
            return JsonData.buildSuccess(0, "ok", "操作成功");
        } else {
            return JsonData.buildError("操作失败");
        }
    }

    /**
     * @Description 用户调出申请
     * @author mzy
     * @time 2018-11-26
     * @see 
     * @return   java.lang.Object
     * @params  []
     */
    @PostMapping("/api/user/transferOut-post")
    public Object userEquipmentTransferOut(EquipmentTransferOutFromPage equipmentTransferOutFromPage,
                                           HttpSession session) {
        boolean res = false;
        try {
            res = transactionalCollectionService.userEquipmentTransferOut(equipmentTransferOutFromPage, session);
        } catch (NoNotExistException e) {
            return JsonData.buildError("设备编号不存在");
        } catch (NoPowerException e) {
            return JsonData.buildError(e.getMessage());
        }

        if (res) {
            return JsonData.buildSuccess(0, "ok", "操作成功");
        } else {
            return JsonData.buildError("操作失败");
        }
    }


}
